<?php
/**
 * Joomla! 1.5 component Joomla Community Polls
 *
 * @version $Id: view.html.php 2009-08-10 03:45:15 svn $
 * @author Maverick
 * @package Joomla
 * @subpackage Joomla Community Polls
 * @license GNU/GPL
 *
 * The Joomla Community Polls allows the members of the Joomla website to create and manage polls from the front-end.
 * The administrator has the powerful tools provided in the back-end to manage the polls published by all users.
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the Joomla Community Polls component
 */
class JoomlacommunitypollsViewPolls extends JView {
	function display($tpl = null) {
        global $mainframe;
        $model = $this->getModel('polls');
        $params = $mainframe->getParams();
        $document =& JFactory::getDocument();
        
        if($this->getLayout() == 'list') {
            $result =& $model->getPollsList();
            $this->assignRef( 'items', $result->items );
            $this->assignRef( 'lists', $result->lists );
            $this->assignRef( 'pagination', $result->pagination );
        }else if($this->getLayout() == 'viewpoll') {
            $poll =& $model->getPollData();

            if (!isset($poll) || ($poll->id > 0 && $poll->published != 1)) {
                JError::raiseError( 403, JText::_('Access Forbidden') );
                return;
            }

            /** Default behavior for all polls */
            $menus	= &JSite::getMenu();
            $menu	= $menus->getActive();
            $pathway  =& $mainframe->getPathway();
            
            if (is_object( $menu )) {
                $menu_params = new JParameter( $menu->params );
                if (!$menu_params->get( 'page_title')) {
                    $params->set('page_title',	$poll->title);
                }
            } else {
                $params->set('page_title',	$poll->title);
            }
            $document->setTitle( $params->get( 'page_title' ) );
            $pathway->addItem($poll->title, '');
            $params->def( 'show_page_title', 1 );
            $params->def( 'page_title', $poll->title );
            /* end default behavior */

            $graphwidth = 350;
            $barheight 	= 15;
            $maxcolors 	= 5;
            $barcolor 	= 0;
            $tabcnt 	= 0;
            $colorx 	= 0;

            $total_votes= $poll->votes;
            $k = 0;
            
            for ($i = 0; $i < count( $poll->options ); $i++){
                $vote =& $poll->options[$i];

                if ($total_votes > 0){
                    $vote->width    = ceil( $vote->votes * $graphwidth / $total_votes );
                    $vote->percent  = round( 100 * $vote->votes / $total_votes, 1 );
                }else{
                    $vote->width	= 0;
                    $vote->percent	= 0;
                }

                $vote->class = '';
                if ($barcolor == 0){
                    if ($colorx < $maxcolors) {
                        $colorx = ++$colorx;
                    } else {
                        $colorx = 1;
                    }
                    $vote->class = "polls_color_".$colorx;
                } else {
                    $vote->class = "polls_color_".$barcolor;
                }

                $vote->barheight = $barheight;

                $vote->odd		= $k;
                $vote->count	= $i;
                $k = 1 - $k;
            }
            
            $this->assignRef('poll', $poll);
        }

        $this->assignRef('params',	$params);

   		parent::display($tpl);
    }
}
?>